package com.hup.mouseIcon.view;

import com.hup.mouseIcon.mvp.MainMvp.MainPresenter;
import com.hup.utils.commons.start.*;
import com.hup.utils.swing.FrameUtil;
import com.hup.utils.swing.FrameUtil.VisibleListener;
import lombok.extern.log4j.Log4j2;

import javax.swing.*;
import java.awt.*;

/**
 * 程序托盘
 * @author hugan
 * @date 2020/1/4
 */
@Log4j2
public class AppTray extends BaseAppTray {
    private static final String CMD_SHOW = "显示";

    private static final String TIP_SHOWING = StartupInfo.getApplicationChName() + ":展示中";
    private static final String TIP_HIDED = StartupInfo.getApplicationChName() + ":已隐藏";

    private JCheckBoxMenuItem cbShowMouse;

    public AppTray(Window window) {
        super(SpringContext.getBean(AppIcon.class).getImage(), TIP_SHOWING);
        MainPresenter presenter = SpringContext.getBean(MainPresenter.class);

        setPopupMenu(getMenu(), e -> presenter.showMouse(cbShowMouse.isSelected()));
        setTrayListener(new TrayActionListener() {
            @Override
            public void beforeShowMenu() {
                cbShowMouse.setSelected(window.isVisible());
            }

            @Override
            public void onClickTray() {
                presenter.showMouse(!window.isVisible());
            }

            @Override
            public void onExit(BaseAppTray appTray) {
                SpringContext.getBean(SimpleExitHandler.class).doExit();
            }
        });

        FrameUtil.addVisibleListener(window, new VisibleListener() {
            @Override
            public void onShown() {
                setToolTip(TIP_SHOWING);
            }

            @Override
            public void onHidden(boolean dispose) {
                setToolTip(TIP_HIDED);
            }
        });
    }

    private SimpleMenu getMenu() {
        cbShowMouse = new JCheckBoxMenuItem(CMD_SHOW);
        cbShowMouse.setState(true);

        SimpleMenu menu = new SimpleMenu();
        menu.addHeader(StartupInfo.getApplicationChName())
                .addHeader(StartupInfo.getVersion())
                .addMenu(cbShowMouse)
                .addExit();
        return menu;
    }

}
